﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlTypes;
using System.Linq;
using System.Text;


namespace nadModel.pad
{
    public partial class MaterialRequisitionProcess : AbstractEntityProcess
    {

        public override AbstractEntity[] PackEntity(object obj)
        {
            //pack entries
            List<MaterialRequisitionEntity> entryList = new List<MaterialRequisitionEntity>();

            MaterialRequisitionEntity entry;
            try
            {
                DataTable dt = (DataTable)obj;
                if (dt == null)
                {
                    return null;
                }

                foreach (DataRow item in dt.Rows)
                {
                    entry = new MaterialRequisitionEntity();
                    entry.IssueNo = item[MaterialRequisitionEntity.ISSUE_NO].ToString();
                    entry.IssueType = item[MaterialRequisitionEntity.ISSUE_TYPE].ToString();
                    entry.ApproveStatus = item[MaterialRequisitionEntity.APPROVE_STATUS].ToString();
                    entry.JobNum = item[MaterialRequisitionEntity.JOB_NUM].ToString();
                    entry.AssemblySeq = item[MaterialRequisitionEntity.ASSEMBLY_SEQ].ToString();
                    entry.MtlSeq = item[MaterialRequisitionEntity.MTL_SEQ].ToString();
                    entry.MtlPartNum = item[MaterialRequisitionEntity.MTL_PARTNUM].ToString();
                    entry.OldPartNum = item[MaterialRequisitionEntity.OLD_PARTNUM].ToString();
                    entry.PartDescription = item[MaterialRequisitionEntity.PART_DESCRIPTION].ToString();
                    entry.RequiredQty = Convert.ToDecimal(item[MaterialRequisitionEntity.REQUIRED_QTY].ToString());
                    entry.ApplyQty = Convert.ToDecimal(item[MaterialRequisitionEntity.APPLY_QTY].ToString());
                    entry.TranQty = Convert.ToDecimal(item[MaterialRequisitionEntity.TRAN_QTY].ToString());
                    entry.NetWeight = Convert.ToDecimal(item[MaterialRequisitionEntity.NET_WEIGHT].ToString());
                    entry.DueDate = string.IsNullOrEmpty(item[MaterialRequisitionEntity.DUE_DATE].ToString()) ? SqlDateTime.Null : Convert.ToDateTime(item[MaterialRequisitionEntity.DUE_DATE].ToString());
                    entry.IUM = item[MaterialRequisitionEntity.IUM_S].ToString();
                    entry.WareHouseCode = item[MaterialRequisitionEntity.WARE_HOUSE_CODE].ToString();
                    entry.WareHouseDesc = item[MaterialRequisitionEntity.WARE_HOUSE_DESC].ToString();
                    entry.BinNum = item[MaterialRequisitionEntity.BIN_NUM].ToString();
                    entry.BinDesc = item[MaterialRequisitionEntity.BIN_DESC].ToString();
                    entry.ProjectCode = item[MaterialRequisitionEntity.PROJECT_CODE].ToString();
                    entry.OnHandQty = Convert.ToDecimal(item[MaterialRequisitionEntity.ON_HAND_QTY].ToString());
                    entry.LossRate = item[MaterialRequisitionEntity.LOSS_RATE].ToString();
                    entry.FeedingTimes = item[MaterialRequisitionEntity.FEEDING_TIMES].ToString();
                    entry.Already = Convert.ToBoolean(item[MaterialRequisitionEntity.ALREADY].ToString());
                    entryList.Add(entry);
                }
            }
            catch (Exception ex)
            {
                Log("ERROR: error packing entity", LogLevel.FATAL);
                Log(ex.ToString(), LogLevel.FATAL);
                throw ex;
            }
            return (AbstractEntity[])entryList.ToArray();

        }
    }
}
